package com.zs.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.zs.model.criteria.BaseCriteria;
import com.zs.service.ResourceService;
import com.zs.service.UserService;

@Controller
@RequestMapping("/resource")
public class ResourceController {

    @Resource
    private Gson gson;

    @Resource
    private ResourceService resourceService;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public String selectResourceLinkFlagWithUserId(HttpServletRequest request,
                                    @RequestParam(value = "pn", required = true, defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "ps", required = true, defaultValue = "3") Integer pageSize,
                                    @RequestParam(value="userId",required=true) String userId
    ) {
        BaseCriteria baseCriteria = new BaseCriteria();
        baseCriteria.setPageNo(pageNo);
        baseCriteria.setPageSize(pageSize);
       
        return resourceService.selectResourceLinkFlagWithUserId(userId,baseCriteria);
    }
    
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public String selectResourceListByUserId(HttpServletRequest request,
                                    @RequestParam(value = "pn", required = true, defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "ps", required = true, defaultValue = "3") Integer pageSize,
                                    @RequestParam(value="userId",required=true) String userId,
                                    @RequestParam(value="resourceId",required=true) String resourceId,
                                    @RequestParam(value="linkFlag",required=true) String linkFlag
    ) {
        if(linkFlag.equals("1")){
        	//就是移除
        	 return resourceService.delLinkUserAndResource(userId, resourceId);
        }else if(linkFlag.equals("0")){
        	//就是增加
        	 return resourceService.addLinkUserAndResource(userId, resourceId);
        }
       
        return null;
       
    }
   
	

    
}
